package model

import (
	"gorm.io/gorm"
	"shop-api/databases/mysql"
)

// UserAddress 用户地址表
type UserAddress struct {
	gorm.Model
	UserID    uint   `gorm:"type:int(10);comment:'用户ID，关联用户表'"`
	Name      string `gorm:"type:varchar(20);comment:'收货人姓名'"`
	Phone     string `gorm:"type:char(11);comment:'收货人手机号'"`
	Province  string `gorm:"type:varchar(10);comment:'省份'"`
	City      string `gorm:"type:varchar(10);comment:'城市'"`
	District  string `gorm:"type:varchar(10);comment:'区/县'"`
	Address   string `gorm:"type:varchar(255);comment:'详细地址'"`
	IsDefault int8   `gorm:"type:tinyint(2);comment:'是否默认地址（0: 否，1: 是）'"`
}

func (u *UserAddress) GetAddrByAddrId(id uint) (*UserAddress, error) {
	return u, mysql.WithClient(func(db *gorm.DB) error {
		return db.Where("id = ?", id).First(&u).Error
	})
}
